package com.maodou.android.ui.page.splash

import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.maodou.android.R
import com.maodou.android.ui.page.LoginKey
import com.maodou.android.ui.page.MainKey
import com.maodou.android.viewmodel.splash.SplashViewModel
import com.maodou.core.navigation.Navigator3
import com.maodou.core.ui.components.AppText
import com.maodou.core.ui.components.VSpacer
import kotlinx.coroutines.delay

@Composable
fun SplashScreen(
    viewModel: SplashViewModel = hiltViewModel()
) {

    val uiState by viewModel.uiState.collectAsStateWithLifecycle()

    LaunchedEffect(viewModel.isAgree.collectAsState().value) {
        if (viewModel.isAgree.value) {
            delay(2000)
            //重置到首页
            if (viewModel.isLogin.value) Navigator3.resetToWithFade(MainKey)
            else Navigator3.resetToWithFade(LoginKey)
        }
    }
    Box(modifier = Modifier.fillMaxSize()) {
//        Image(
//            painter = painterResource(R.mipmap.ic_top_bg),
//            contentDescription = null,
//            contentScale = ContentScale.FillBounds,
//            modifier = Modifier.fillMaxWidth()
//        )

        Column(
            modifier = Modifier
                .align(Alignment.Center)
                .padding(horizontal = 32.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            ShimmerText(
                text = stringResource(R.string.app_name),
                textColor = Color.Black,
                highlightColor = Color.White,
                fontSize = 24.sp
            )
        }

        Column(
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(40.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(
                painter = painterResource(R.mipmap.ic_logo),
                contentDescription = null,
                contentScale = ContentScale.FillBounds,
                modifier = Modifier
                    .size(64.dp)
                    .clip(RoundedCornerShape(8.dp)),
            )
            VSpacer(4.dp)
            AppText(
                text = uiState.appName,
                color = MaterialTheme.colorScheme.onSurfaceVariant,
            )
        }

        if (!viewModel.isAgree.collectAsState().value) {
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) {
                PrivacyPolicyUI(viewModel)
            }
        }


    }
}

@Composable
fun ShimmerText(
    text: String,
    textColor: Color = Color.Black,
    highlightColor: Color = MaterialTheme.colorScheme.primary,
    shimmerAngle: Float = 20f, // 闪光角度
    durationMillis: Int = 1400,
    fontSize: TextUnit = 32.sp,
    fontWeight: FontWeight = FontWeight.Bold
) {
    val transition = rememberInfiniteTransition(label = "shimmer")

    val shimmerOffsetX by transition.animateFloat(
        initialValue = -400f,
        targetValue = 800f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis, easing = LinearEasing)
        ),
        label = "offset"
    )

    val shimmerBrush = Brush.linearGradient(
        colors = listOf(
            textColor.copy(alpha = 0.3f),
            highlightColor.copy(alpha = 0.9f),
            textColor.copy(alpha = 0.3f)
        ),
        start = Offset(shimmerOffsetX, 0f),
        end = Offset(shimmerOffsetX + 300f, 300f)
    )

    Text(
        text = text,
        style = TextStyle(
            brush = shimmerBrush,
            fontSize = fontSize,
            fontWeight = fontWeight,
            shadow = Shadow(
                color = Color.White.copy(alpha = 0.2f),
                offset = Offset(1f, 1f),
                blurRadius = 2f
            )
        ),
        modifier = Modifier
    )
}
